package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.BlogDTO;
import com.sky.dto.BlogPageQueryDTO;
import com.sky.entity.Blog;
import com.sky.entity.ManageBlog;
import com.sky.mapper.BlogLikeMapper;
import com.sky.mapper.BlogMapper;
import com.sky.mapper.ManageBlogMapper;
import com.sky.result.PageResult;
import com.sky.service.BlogService;
import com.sky.utils.BlogConverter;
import com.sky.vo.BlogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogLikeMapper blogLikeMapper;

    @Autowired
    private ManageBlogMapper manageBlogMapper;

    /**
     * 博客分页查询
     * @param blogPageQueryDTO
     * @return
     */
    public PageResult pageQuery(BlogPageQueryDTO blogPageQueryDTO) {
        PageHelper.startPage(blogPageQueryDTO.getPage(), blogPageQueryDTO.getPageSize());
        blogPageQueryDTO.setUserId(BaseContext.getCurrentId());
        Page<BlogVO> page =blogMapper.pageQuery(blogPageQueryDTO);



        long total = page.getTotal();
        List<BlogVO> list = page.getResult();

        return new PageResult(total,list);
    }

    /**
     * 根据id查询论坛
     * @param blogid
     * @return
     */
    public Blog getById(Long blogid) {
        Blog blog = blogMapper.getById(blogid);
        return blog;
    }

    /**
     * 根据用户id查询论坛
     * @param blogPageQueryDTO
     * @return
     */
    public PageResult getByUserId(BlogPageQueryDTO blogPageQueryDTO) {
        PageHelper.startPage(blogPageQueryDTO.getPage(), blogPageQueryDTO.getPageSize());

        Page<BlogVO> page =blogMapper.pageQuery(blogPageQueryDTO);

        long total = page.getTotal();
        List<BlogVO> list = page.getResult();

        return new PageResult(total,list);
    }

    /**
     * 新增博客
     * @param blogDTO
     */
    public void save(BlogDTO blogDTO) {
        Blog blog =new Blog();
        BeanUtils.copyProperties(blogDTO,blog);
        blogMapper.insert(blog);
        ManageBlog manageBlog = BlogConverter.fromBlog(blog);
        manageBlogMapper.insert(manageBlog);

    }

    /**
     * 增加浏览量
     * @param id
     */
    public void addPageView(Long id) {
        blogMapper.addPageView(id);
        manageBlogMapper.addPageView(String.valueOf(id));
    }

    /**
     * 增加点赞数
     * @param blogDTO
     */
    public void addLikeCount(BlogDTO blogDTO) {
        blogMapper.addLikeCount(blogDTO);
        blogLikeMapper.addLikeCount(blogDTO);
        Blog blog =new Blog();
        BeanUtils.copyProperties(blogDTO,blog);
        ManageBlog manageBlog = BlogConverter.fromBlog(blog);
        manageBlogMapper.addLikeCount(manageBlog);
    }

    /**
     * 取消点赞
     * @param blogDTO
     */
    public void deleteLikeCount(BlogDTO blogDTO) {
        blogMapper.deleteLike(blogDTO);
        blogLikeMapper.deleteLike(blogDTO);
        Blog blog =new Blog();
        BeanUtils.copyProperties(blogDTO,blog);
        ManageBlog manageBlog = BlogConverter.fromBlog(blog);
        manageBlogMapper.deleteLike(manageBlog);
    }
}
